import {
  myPlan,
  myPressure,
  plans,
  pressures,
  daysofplan,
  daysofpressure
} from '../../api/index.js'


Page({
  /**
   * 页面的初始数据
   */
  data: {
    modalHidden: true,
    src: '',
    userPlans: [],
    userPressures: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getMyPlan();
    this.getMyPressure();
  },

  //分享
  onShareAppMessage: function () {
    return {
      title: '我正在使用SMART睡眠训练计划，超有用！推荐给你',
      imageUrl: '../../images/98875.png'

    }
  },

  // 点击某一个计划
  handlePlanDetail(e) {
    let planId = e.currentTarget.dataset.item.id
    let status = e.currentTarget.dataset.item.status;
    if (status == 1) {
      daysofplan().then(res => {
        if (res.errcode = '00000') {
          wx.setStorageSync('entity', res.days.find(day => day.id === res.currentDayId))
          wx.navigateTo({
            url: '/pages/schedule/schedule'
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/course-description/course-description?planId=' + planId,
      })
    }

  },

  // 点击某一个压力课程
  handlePressureDetail(e) {
    let pressureId = e.currentTarget.dataset.item.id;
    let status = e.currentTarget.dataset.item.status;
    if (status == 1) {
      daysofpressure().then(res => {
        if (res.errcode = '00000') {
          wx.setStorageSync('entity', res.days.find(day => day.id === res.currentDayId))
          wx.navigateTo({
            url: '/pages/pressure-schedule/pressure-schedule'
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/pressure-description/pressure-description?planId=' + pressureId,
      })
    }
  },

  getMyPlan() {
    myPlan().then(res => {
      if (res.errcode == '00000') {
        let userPlanList = res.user_plans
        let arr = []
        userPlanList.forEach(item => {
          plans(item.weapp_plan).then(result => {
            result.status = item.status
            if (result.days == 7) {
              result.src = '../../images/plan_7.png'
              result.color = ''
              result.date = item.created_at.split('T')[0]
            }
            if (result.days == 14) {
              result.src = '../../images/plan_21.png'
              result.color = 'yellow'
              result.date = item.created_at.split('T')[0]
            }
            if (result.days == 28) {
              result.src = '../../images/plan_28.png'
              result.color = 'orange'
              result.date = item.created_at.split('T')[0]
            }
            arr.push(result)
            this.setData({
              userPlans: arr
            })
            console.log(this.data.userPlans, 'userPlans')
          })
        })
      }
    })
  },

  getMyPressure() {
    myPressure().then(res => {
      if (res.errcode == '00000') {
        let userPressureList = res.user_pressures
        let arr = []
        userPressureList.forEach(item => {
          pressures(item.weapp_pressure).then(result => {
            result.status = item.status
            result.src = '../../images/plan_7.png'
            result.color = ''
            result.date = item.created_at.split('T')[0]
            arr.push(result)
            this.setData({
              userPressures: arr
            })
            console.log(this.data.userPressures, 'userPlans')
          })
        })
      }
    })
  },

})